//
//  main.c
//  C_12线程
//
//  Created by Robin on 16/3/22.
//  Copyright © 2016年 cnbm. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

int beers = 2000000;
pthread_mutex_t a_lock = PTHREAD_MUTEX_INITIALIZER;


void error (char *msg){
    fprintf(stderr, "%s:%s\n",msg,strerror(errno));
    exit(1);
}
// void * 指针和 long 大小相同
void * do_stuff (void *param){
    long thread_no = (long)param;
    printf("Thread no is %ld\n",thread_no);
    
    return (void *)(thread_no + 1); // 返回时将其类型转化为指针
}

//void *drink_losts (void *a){
//    
//    for (int i=0; i<10000; i++) {
//        beers = beers -1;
//    }
//    
//    return NULL;
//}

int main(int argc, const char * argv[]) {

    pthread_t threads[3];
    long t;
    
    for (t=0; t<3; t++) {
        
        if (pthread_create(&threads[t], NULL, do_stuff, (void *)t) == -1) {
            printf("线程创建失败 %ld\n",t);
        }
    }
    
    void *result;
    
    for (int i=0; i<3; i++) {
        if (pthread_join(threads[i], &result) == -1) {
            printf("回收线程失败");
        }
        
        printf("Thread %d returned %ld\n",i,(long)result);
    }
    
    return 0;
}
